<?php
/*test*/
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Book {
    //put your code here
    var $name;
	var $face;
	var $introduce;
	var $tag;
	var $top;
	var $down;
	
	function Book($bname,$bface,$bintro,$btag,$btop,$bdown)
	{
		$this->name = $bname;
		$this->face = $bface;
		$this->introduce = $bintro;
		$this->tag = $btag;
		$this->top = $btop;
		$this->down = $bdown;
	}
	
	static function findBookByName($bookname)
	{
		$DB = mysql_connect("localhost","root","12345");
		if(!$DB){
            die('Could not connect:  '.mysql_error());
        }
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		$DBT = mysql_query("select * from books where name = '$bookname'");
		
		if(mysql_num_rows($DBT) == 0)
		{
			die("no book.");
		}
		else 
		{
			$DBTrow = mysql_fetch_array($DBT);
			$bk = new Book($DBTrow["name"],$DBTrow["faceurl"],
							$DBTrow["introduction"],$DBTrow["tags"],$DBTrow["tops"],$DBTrow["downs"]);
		}
		return $bk;
	}
	
	static function noBook($bookname)
	{
		$DB = mysql_connect("localhost","root","12345");
		if(!$DB){
            die('Could not connect:  '.mysql_error());
        }
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		$DBT = mysql_query("select * from books where name = '$bookname'");
		
		if(mysql_num_rows($DBT) == 0)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	static public function findBookLikeName($value)
	{
		$DB = mysql_connect("localhost","root","12345");
		if(!$DB){
            die('Could not connect:  '.mysql_error());
        }
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		$DBT = mysql_query("select * from books where name like '%$value%'");
		if(mysql_num_rows($DBT) == 0)
		{
			die("no book.");
		}
		else 
		{
			$list = array();
			for($count = 0;$count < mysql_num_rows($DBT);$count++)
			{
				$DBTrow = mysql_fetch_array($DBT);
				$bk = new Book($DBTrow["name"],$DBTrow["faceurl"],
								$DBTrow["introduction"],$DBTrow["tags"],$DBTrow["tops"],$DBTrow["downs"]);
				array_push($list,$bk);
			}
			// echo count($list);
			return $list;
		}
	}
	
	static function randomBook($num)
	{
		$DB = mysql_connect("localhost","root","12345");
		if(!$DB){
            die('Could not connect:  '.mysql_error());
        }
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		
		$DBT = mysql_query("select * from books order by rand() limit 0,$num");
		// echo mysql_num_rows($DBT);
		if(mysql_num_rows($DBT) == 0)
		{
			die("no book.");
		}
		$list = array();
		for($count = 0;$count < mysql_num_rows($DBT);$count++)
		{
			$DBTrow = mysql_fetch_array($DBT);
			$bk = new Book($DBTrow["name"],$DBTrow["faceurl"],
							$DBTrow["introduction"],$DBTrow["tags"],$DBTrow["tops"],$DBTrow["downs"]);
			array_push($list,$bk);
		}
		// echo count($list);
		return $list;
	}
	
	static function clearTag($bookname)
	{
		$DB = mysql_connect("localhost","root","12345");
		if(!$DB){
            die('Could not connect:  '.mysql_error());
        }
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		mysql_query("update books set tags = '' where name = '$bookname'");
	}
	
	public static function addBook($delname,$furl,$intro,$tag)
	{
		$DB = mysql_connect("localhost","root","12345");
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		mysql_query("INSERT INTO books (name,faceurl,introduction,tags) VALUES ('$delname','$furl','$intro','$tag')"); 
	}
	
	public static function deleteBook($delname)
	{
		$DB = mysql_connect("localhost","root","12345");
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		
		mysql_query("delete from books where name = '$delname'");
		mysql_query("delete from comments where bookname = '$delname'");
		mysql_query("delete from notes where bookname = '$delname'");
	}
	
	function getName()
	{
		return $this->name;
	}
	
	function getFace()
	{
		return $this->face;
	}
	
	function getIntro()
	{
		return $this->introduce;
	}
	
	function getTag()
	{
		$arr = explode("&&", $this->tag);
		return $arr;
	}
	
	function addTag($newtag)
	{
		$DBT = mysql_query("select tags from books where name = '$this->name'");
		$DBTrow = mysql_fetch_array($DBT);
		$oldtag = $DBTrow["tags"];
		$arr = explode("&&", $oldtag);
		array_push($arr,$newtag);
		$update = implode("&&", $arr);
		mysql_query("update books set tags = '$update' where name = '$this->name'");
		$this->tag = $update;
	}
	
	function getTop()
	{
		return $this->top;
	}
	
	function getDown()
	{
		return $this->down;
	}
	
	function addTop()
	{
		mysql_query("update books set tops = tops + 1 where name = '$this->name'");
		$this->top++;
	}
	
	function addDown()
	{
		mysql_query("update books set downs = downs + 1 where name = '$this->name'");
		$this->down++;
	}
}

?>
